package com.tuanzi.loan.business.service.system.impl;

import static com.google.common.base.Preconditions.checkArgument;

import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tuanzi.loan.business.entity.system.DataPermission;
import com.tuanzi.loan.business.repository.system.DataPermissionRelationRepository;
import com.tuanzi.loan.business.repository.system.DataPermissionRepository;
import com.tuanzi.loan.business.service.system.DataPermissionService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.exception.SystemException;
import com.tuanzi.loan.core.repository.JPABaseRepository;
import com.tuanzi.loan.core.service.BaseServiceImpl;

/**
 * @author Ray 2017/09/21
 */
@Debug
@Service
@Transactional
@Retryable(value = { RetryException.class })
public class DataPermissionServiceImpl extends BaseServiceImpl<DataPermission, String> implements DataPermissionService {

	@Autowired
	private DataPermissionRepository dataPermissionRepository;
	@Autowired
	private DataPermissionRelationRepository dataPermissionRelationRepository;

	@Override
	protected JPABaseRepository<DataPermission, String> getRepository() {
		return dataPermissionRepository;
	}

	@Override
	public DataPermission saveOrUpdate(DataPermission dataPermission) {

		List<DataPermission> existingPermissions = dataPermissionRepository.findByCode(dataPermission.getCode());

		if (CollectionUtils.isNotEmpty(existingPermissions)) {

			checkArgument(existingPermissions.size() == 1);

			String existingPermissionId = existingPermissions.get(0).getId();
			if (StringUtils.isBlank(dataPermission.getId()) || !StringUtils.equals(existingPermissionId, dataPermission.getId())) {
				throw new SystemException("已存在相同的权限编码");
			}

		}
		return super.saveOrUpdate(dataPermission);
	}

	@Override
	public void movePermission(DataPermission from, DataPermission to) {
		from.setParentId(to.getId());
		this.saveOrUpdate(from);
	}

	@Override
	public void recurseDelete(DataPermission root) {
		checkArgument(root != null && root.getId() != null);

		List<DataPermission> dataPermissions = dataPermissionRepository.findAllByParentId(root.getId());

		if (CollectionUtils.isNotEmpty(dataPermissions)) {
			dataPermissions.parallelStream().forEach(this::recurseDelete);
		}
		dataPermissionRelationRepository.deleteInBatch(dataPermissionRelationRepository.findAllByPermissionId(root.getId()));
		dataPermissionRepository.delete(root.getId());
	}

	@Override
	public List<DataPermission> findAllPermissionByUserId(String userId) {
		return dataPermissionRepository.findAllPermissionByUserId(userId);
	}
}
